import { vPages } from "@/views";
const viewComps = import.meta.glob("../views/**/index.vue");

const transformObjToRoute = (list: RouterType.RouteItem[], id: number) => {
  return list
    .filter((item) => item.meta.pid == id)
    .map((item) => {
      item.children = transformObjToRoute(list, item.meta.id);
      return item;
    });
};

export const viewRoutes: RouterType.RouteItem[] = Object.entries(viewComps)
  .map(([path, component]) => {
    path = path.replace("../views", "").replace("/index.vue", "") || "/";
    const name = path.split("/").filter(Boolean).join("-") || "index";
    if (vPages[name]) {
      if (vPages[name].dynamicRoute && vPages[name].dynamicPath) {
        return {
          path: vPages[name].dynamicPath,
          name,
          component,
          meta: vPages[name],
        };
      }
      if (vPages[name].redirect) {
        return {
          path,
          name,
          component,
          meta: vPages[name],
          redirect: vPages[name].redirect,
        };
      } else {
        return {
          path,
          name,
          component,
          meta: vPages[name],
        };
      }
    } else {
      console.warn("@/views/index.ts未配置属性:" + name);
    }
  })
  .filter(Boolean);

export const levelRoutes: RouterType.RouteItem[] = transformObjToRoute(
  viewRoutes,
  0
);
console.log(levelRoutes);
